;=============================================================================
; $VER: Install_Roadshow 1.12 (24.4.2017)
;=============================================================================

;=============================================================================
; Local procedures
;=============================================================================

; Make a backup copy of the Roadshow configuration files
(procedure P_MakeBackup
(
	(message (cat
		"This script will make backup copies of the configuration files "
		"stored in 'DEVS:NetInterfaces', 'DEVS:Internet', 'SYS:Storage/NetInterfaces', "
		"'S:IPF' and 'S:PPP-Configurations'."
		"\n\n"
		"These copies will be stored in a drawer whose name you will be prompted to "
		"enter."
	))

	; Select or make a new directory to store the backup copy in
	(set #dir-acceptable 0)
	(while (not #dir-acceptable)
	(
		(set #backup-dir (askdir
			(prompt (cat
				"Please select or make a new drawer in which backup copies of the "
				"Roadshow configuration files should be stored!"
			))
			(newpath)
			(help @askdir-help)
			(default (tackon @default-dest "Roadshow-Config-Backup"))
		))
		(set #backup-dir-exists (exists #backup-dir))
		
		(if (= 0 #backup-dir-exists)
		(
			(set #makedir-success 0)

			(trap 4
				(set #makedir-success
					(makedir #backup-dir (infos)
				))
			)

			(if #makedir-success
			(
				(set #dir-acceptable 1)
			)
			(
				(message (cat
					"The drawer '" #backup-dir "' did not already exist and could not be "
					"created either. Please try again and select or make a new drawer."
				))
			))
		))

		(if (= 1 #backup-dir-exists)
		(
			(message (cat
				"Please select or make a new drawer; '" #backup-dir "' already exists."
			))
		))

		(if (= 2 #backup-dir-exists)
		(
			; That's good enough for us
			(set #dir-acceptable 1)
		))
	))

	; Create the respective subdirectories first; the
	; (copyfiles) command cannot do this all by itself.
	(makedir (tackon #backup-dir "DEVS") (infos))
	(makedir (tackon #backup-dir "Storage") (infos))
	(makedir (tackon #backup-dir "S") (infos))

	; Now start copying...
	(copyfiles
		(source "DEVS:NetInterfaces")
		(dest (tackon #backup-dir "DEVS/NetInterfaces"))
		(all)
		(infos)
	)
	(copyfiles
		(source "DEVS:Internet")
		(dest (tackon #backup-dir "DEVS/Internet"))
		(all)
	)
	(copyfiles
		(source "SYS:Storage/NetInterfaces")
		(dest (tackon #backup-dir "Storage/NetInterfaces"))
		(all)
		(infos)
	)
	(copyfiles
		(source "S:IPF")
		(dest (tackon #backup-dir "S/IPF"))
		(all)
		(infos)
	)
	(copyfiles
		(source "S:PPP-Configurations")
		(dest (tackon #backup-dir "S/PPP-Configurations"))
		(all)
		(infos)
	)

	(message (cat
		"Backup copies of your Roadshow configuration files were "
		"stored in '" #backup-dir "'."
	))
))

;=============================================================================

; Uninstall Roadshow
(procedure P_Uninstall
(
	(if (askbool
		(prompt (cat
			"If you choose to proceed, this script will attempt to remove the "
			"Roadshow installation by deleting the files that were copied to your "
			"system partition when Roadshow was installed. Which files were "
			"installed is described in the Roadshow documentation."
			"\n\n"
			"If you made any modifications to these files since they were installed "
			"you might want to verify that they can be safely deleted, or maybe make "
			"a backup copy of the relevant files."
			"\n\n"
			"Note that uninstalling Roadshow will not remove the directory in which "
			"the documentation was installed."
			"\n\n"
			"Do you want to uninstall Roadshow?"
		))
		(help (cat
			"Unless you renamed or moved any of the files Roadshow installed on "
			"your system partition, your system partition is write-protected, or "
			"you are using a non-standard set of assignments pointing to your system "
			"partition, this script will delete the files installed for Roadshow."
			"\n\n"
			@askbool-help
		))
	)
	(
		; Delete the shell commands
		(delete "C:AddNetInterface")
		(delete "C:AddNetRoute")
		(delete "C:arp")
		(delete "C:ConfigureNetInterface")
		(delete "C:DeleteNetRoute")
		(delete "C:ftp")
		(delete "C:GetNetStatus")
		(delete "C:ipf")
		(delete "C:ipfstat")
		(delete "C:ipmon")
		(delete "C:ipnat")
		(delete "C:NetLogViewer")
		(delete "C:NetShutdown")
		(delete "C:ping")
		(delete "C:ppp_connector")
		(delete "C:ppp_dialer")
		(delete "C:ppp_sample")
		(delete "C:RemoveNetInterface")
		(delete "C:RoadshowControl")
		(delete "C:SampleNetSpeed")
		(delete "C:ShowNetStatus")
		(delete "C:tcpdump")
		(delete "C:traceroute")
		(delete "C:wget")

		; Delete the Internet configuration files
		(run "Delete DEVS:Internet ALL QUIET")
		(delete "DEVS:Internet.info")
		
		; Delete the interface configuration files
		(run "Delete DEVS:NetInterfaces ALL QUIET")
		(delete "DEVS:NetInterfaces.info")
		(run "Delete SYS:Storage/NetInterfaces ALL QUIET")
		(delete "SYS:Storage/NetInterfaces.info")
		
		; Delete the PPP/PPPoE drivers
		(delete "DEVS:Networks/ppp-ethernet.device")
		(delete "DEVS:Networks/ppp-serial.device")

		; Delete the Roadshow libraries
		(delete "LIBS:bsdsocket.library")
		(delete "LIBS:usergroup.library")

		; Delete the catalog files
		(delete "LOCALE:catalogs/deutsch/bsdsocket.catalog")
		(delete "LOCALE:catalogs/deutsch/ppp-ethernet.catalog")
		(delete "LOCALE:catalogs/deutsch/ppp-serial.catalog")
		(delete "LOCALE:catalogs/deutsch/roadshow.catalog")
		(delete "LOCALE:catalogs/deutsch/usergroup.catalog")
		(delete "LOCALE:catalogs/polski/bsdsocket.catalog")
		(delete "LOCALE:catalogs/polski/ppp-ethernet.catalog")
		(delete "LOCALE:catalogs/polski/ppp-serial.catalog")
		(delete "LOCALE:catalogs/polski/roadshow.catalog")
		(delete "LOCALE:catalogs/polski/usergroup.catalog")

		; Delete the script files
		(delete "S:Check-Firewall-Rules")
		(delete "S:Network-Startup")
		(delete "S:Start-Firewall")
		(delete "S:Stop-Firewall")

		; Delete the example files for the firewall and
		; the PPP/PPPoE configuration
		(run "Delete S:IPF ALL QUIET")
		(run "Delete S:PPP-Configurations ALL QUIET")

		(message "All files installed by Roadshow were deleted.")
	))
))

;=============================================================================

; Install Roadshow for real
(procedure P_Install
(
	(complete 0)

	; Ask if the '020 versions should be installed. This question will pop up
	; if the machine this script runs on has no MC68000/MC68010 CPU installed, or
	; the script runs in expert mode. In the latter case, the user gets to make
	; the decision.
	(set #special-version 0)

	; Is there an '020 version available in the first place?
	; The demo version does not ship with one...
	(if (= (exists "Workbench/Libs/020/bsdsocket.library" (noreq)) 1)
	(
		; Check if the local CPU can be used with the '020-optimized library.
		(set #local-cpu-type (database "cpu"))
		(set #local-cpu-ok (not (or (= #local-cpu-type "68000") (= #local-cpu-type "68010"))))

		; Note: The following only works on Installer versions 43 and up. Older
		; versions use the SAS/C built-in astcsma() function which cannot handle
		; the '|' switch.
		; (set #local-cpu-ok (not (patmatch "68000|68010" (database "cpu"))))

		(if (or #local-cpu-ok (= @user-level 2))
		(
			; Add a stern warning in case the choice is not obvious.
			(if #local-cpu-ok
			(
				(set #special-warning "")
			)
			(
				(set #special-warning (cat
					"\n\n"
					"NOTE: These special versions will not work on the Amiga you "
					"are currently trying to install the software on. Installation is "
					"therefore discouraged unless you really know what you are doing!"
				))
			))

			(set #special-version (askbool
				(prompt (cat
					"Would you like to install special versions of the Roadshow "
					"'bsdsocket.library' and the PPP/PPPoE drivers, optimized for your Amiga system?"
					#special-warning
				))
				(default #local-cpu-ok)
				(help (cat
					"Special versions of 'bsdsocket.library' and the PPP/PPPoE drivers are provided which were "
					"created for use on Amigas equipped with an MC68020 CPU "
					"or a more recent model. These versions can perform "
					"faster than the generic versions and are also a bit smaller.\n\n"
					"Note that these special versions will not work on Amigas with "
					"plain MC68000 or MC68010 CPUs installed."
				))
			))
		))
	))

	; Is the 'Documentation' drawer actually around? If you use a
	; stripped-down installation archive, to be copied to an 880K
	; floppy disk, you might want to omit the documentation
	; altogether.
	(if (= (exists "Documentation" (noreq)) 2)
	(
		; In "expert" mode, ask if the Roadshow documentation should
		; be installed. In "average" mode the documentation is always
		; installed without prompting.
		(set #install-documentation 1)

		(if (= @user-level 2)
		(
			(set #install-documentation (askbool
				(prompt (cat
					"Would you like to install the Roadshow documentation as "
					"well as the TCP/IP stack software?"
				))
				(default 1)
				(help (cat
					"You may choose to omit installing the Roadshow documentation if all "
					"you require is the TCP/IP stack software and its utilities. The "
					"documentation is always available from the installation archive or CD-ROM, "
					"so you are not missing out on anything."
				))
			))
		))
	)
	(
		; No documentation available for installation
		(set #install-documentation 0)
	))

	; Same thing as with the documentation: it is possible to omit the
	; PPP/PPPoE drivers from the installation disk, and if they are not
	; available for installation, then no such attempt should be made.
	(if (= (exists "Workbench/Devs/Networks" (noreq)) 2)
	(
		; In "expert" mode, ask if the PPP/PPPoE drivers should
		; be installed. In "average" mode the drivers are
		; always installed without prompting.
		(set #install-ppp 1)

		(if (= @user-level 2)
		(
			(set #install-ppp (askbool
				(prompt (cat
					"Would you like to install the PPP and PPPoE drivers as "
					"well as the TCP/IP stack software?"
				))
				(default 1)
				(help (cat
					"You may choose to omit installing the PPP and PPPoE drivers and their "
					"example configuration files if you do not need them.\n\n"
					"The PPP driver is useful for connecting your Amiga through a modem or ISDN adaptor. "
					"The PPPoE driver is used for connecting your Amiga through an ADSL modem.\n\n"
					"You may not need either driver if you only intend to connect your Amiga through an "
					"Ethernet card, for example.\n\n"
					"Note that if you omit the PPP/PPPoE drivers and find that you need them later, you "
					"will have to copy them manually to your system partition."
				))
			))
		))
	)
	(
		; No PPP/PPPoE drivers available for installation
		(set #install-ppp 0)
	))

	; If the documentation is to be installed, create a drawer
	; to store it in.
	(if #install-documentation
	(
		(set #dir-created 0)

		(while (not #dir-created)
		(
			; Ask for the installation directory
			(set #default-dest (askdir
				(prompt (cat
					"Please select where the documentation for Roadshow should be installed. "
					"A drawer ``Roadshow'' will be created there."
				))
				(help @askdir-help)
				(default @default-dest)
			))

			(set #doc-dir-name (tackon #default-dest "Roadshow"))

			(set #makedir-success 0)

			(trap 4
				(set #makedir-success
					(makedir #doc-dir-name (infos)
				))
			)

			(if #makedir-success
			(
				(set @default-dest #doc-dir-name)
				(set #dir-created 1)
			)
			(
				(message (cat
					"The drawer '" #doc-dir-name "' could not be created. Please try "
					"again and select a new drawer."
				))
			))
		))
	))

	(complete 10)

	; Begin by copying the documentation, if desired.
	(if #install-documentation
	(
		(copyfiles
			(source "Documentation")
			(dest @default-dest)
			(infos)
			(all)
		)
	)
	(
		; So the final report on where the software was installed
		; will not be entirely misleading.
		(set @default-dest "SYS:")
	))

	(complete 20)

	; Copy the shell commands
	(copyfiles
		(source "Workbench/C")
		(dest "C:")
		(files)
		(all)
	)

	; Oops, remove the shell commands relevant for the PPP/PPPoE
	; drivers again (that's easier than just copying the files
	; one at a time).
	(if (not #install-ppp)
	(
		(delete "C:ppp_connector")
		(delete "C:ppp_dialer")
		(delete "C:ppp_sample")
	))

	; Copy the Internet configuration files
	(makedir "DEVS:Internet" (infos))
	(copyfiles
		(source "Workbench/Devs/Internet")
		(dest "DEVS:Internet")
		(files)
		(infos)
		(all)
	)

	(complete 30)

	; Add an empty network interfaces drawer
	; in "DEVS:".
	(makedir "DEVS:NetInterfaces" (infos))

	; Make sure that the "SYS:Storage" drawer exists,
	; which may not be the case for Workbench 2.x.
	(makedir "SYS:Storage" (infos))

	; Copy the network interface example files.
	(makedir "SYS:Storage/NetInterfaces" (infos))
	(copyfiles
		(source "Workbench/Storage/NetInterfaces")
		(dest "SYS:Storage/NetInterfaces")
		(files)
		(infos)
		(all)
	)

	; Remove the PPP/PPPoE configuration files, if necessary
	(if (not #install-ppp)
	(
		(delete "SYS:Storage/NetInterfaces/PPP")
		(delete "SYS:Storage/NetInterfaces/PPP.info")
		(delete "SYS:Storage/NetInterfaces/PPPoE")
		(delete "SYS:Storage/NetInterfaces/PPPoE.info")
	))

	(complete 40)

	; Before the library is copied, make sure that we pick
	; the right type.
	(if (and #special-version (= (exists "Workbench/Libs/020/bsdsocket.library" (noreq)) 1))
	(
		(set #lib-path "Workbench/Libs/020/bsdsocket.library")
	)
	(
		(set #lib-path "Workbench/Libs/bsdsocket.library")
	))

	; Copy the bsdsocket and usergroup libraries
	(copyfiles
		(source #lib-path)
		(dest "LIBS:")
	)
	(copyfiles
		(source "Workbench/Libs/usergroup.library")
		(dest "LIBS:")
	)

	; Copy the PPP/PPPoE drivers
	(if (and #install-ppp (= (exists "Workbench/Devs/Networks" (noreq)) 2))
	(
		(if (and #special-version (= (exists "Workbench/Devs/Networks/020" (noreq)) 2))
		(
			(set #dev-path "Workbench/Devs/Networks/020")
		)
		(
			(set #dev-path "Workbench/Devs/Networks")
		))
		(makedir "DEVS:Networks")
		(copyfiles
			(source (tackon #dev-path "ppp-serial.device"))
			(dest "DEVS:Networks")
		)
		(copyfiles
			(source (tackon #dev-path "ppp-ethernet.device"))
			(dest "DEVS:Networks")
		)
	))

	(complete 50)

	; Copy the locale catalog files, if possible
	(if (= 2 (exists "LOCALE:Catalogs" (noreq)))
	(
		(copyfiles
			(source "Workbench/Locale/Catalogs")
			(dest "LOCALE:Catalogs")
			(all)
		)
	))

	(complete 60)

	; Copy the firewall example configurations
	(copyfiles
		(source "Workbench/S/IPF")
		(dest "S:IPF")
		(all)
	)

	(complete 70)

	; Copy the firewall scripts
	(copyfiles
		(source "Workbench/S/Check-Firewall-Rules")
		(dest "S:")
	)
	(copyfiles
		(source "Workbench/S/Start-Firewall")
		(dest "S:")
	)
	(copyfiles
		(source "Workbench/S/Stop-Firewall")
		(dest "S:")
	)

	(complete 80)

	; Copy the PPP/PPPoE example configurations
	(if (and #install-ppp (= (exists "Workbench/S/PPP-Configurations" (noreq)) 2))
	(
		(copyfiles
			(source "Workbench/S/PPP-Configurations")
			(dest "S:PPP-Configurations")
			(all)
		)
	))

	; Copy the network startup script
	(copyfiles
		(source "Workbench/S/Network-Startup")
		(dest "S:")
	)

	(complete 90)

	; Fix a typo in the S:User-Startup script, which would cause the network
	; startup not to be invoked.
	(if (= (exists "Install-Extras/SearchReplace" (noreq)) 1)
	(
		(run (cat "Install-Extras/SearchReplace from S:User-Startup inplace "
		          "search \"If EXIST S:Network-Startup\" "
		          "replace \"If EXISTS S:Network-Startup\"")
		     (help "If you previously gave the original demonstration version "
		           "of Roadshow a test, your S:User-Startup file may contain a "
		           "typo which has the effect of not making the network start "
		           "properly. This problem can be fixed here, automatically."
		     )
		)
	))

	; Modify the "S:User-Startup" script to invoke the network
	; startup script
	(startup "Roadshow"
		(command (cat
			"If EXISTS S:Network-Startup\n"
			"  Execute S:Network-Startup\n"
			"EndIf"
		))
		(prompt (cat
			"In order to start the network before the Workbench opens, a script "
			"must be run. Instructions to run this script should be added "
			"to your 'S:User-Startup' file."
			"\n\n"
			"If you skip this part, then you will need to execute the script "
			"file 'S:Network-Startup' manually each time you want to start the network."
		))
		(help @startup-help)
	)

	; Make sure that the information about the documentation makes sense:
	; don't say anything about the documentation unless it was actually
	; installed.
	(if #install-documentation
	(
		(set #documentation-notes (cat
			"Please see the documentation in '" (tackon @default-dest "installation.guide" ) "' for a quick "
			"introduction to configuring Roadshow. "
			"More detailed documentation on Roadshow can be found in the same drawer.\n\n"
		))
	)
	(
		(set #documentation-notes (cat
			"Please refer to the 'installation.guide' documentation for a quick introduction "
			"to configuring Roadshow.\n\n"
		))
	))

	(message (cat
		"Your Roadshow TCP/IP stack installation is complete.\n\n"
		"Note that no network interfaces are configured yet, which means that "
		"you cannot access the local network or the Internet yet.\n\n"
		#documentation-notes
		"Please reboot your Amiga to start Roadshow."
	))
))

;=============================================================================
; Start of installation script
;=============================================================================
(complete 0)

(if (< (/ @installer-version 65536) 43)
(
	(abort (cat
		"This installation script requires the Installer tool V43"
	))
))

(if (< (getversion) (* 37 65536))
	(abort (cat
		"Roadshow requires operating system "
		"version 2.04 or higher to work."
	))
)

; Could this be AmigaOS4? Then it doesn't need Roadshow 68k
; to be installed.
(if (>= (getversion) (* 50 65536))
(
	(if (not (askbool
		(prompt (cat
			"WARNING:\n\n"
			"Your Amiga appears to be running Amiga operating system version 4 or higher.\n\n"
			"This operating system already ships with the Roadshow TCP/IP stack installed, and "
			"attempting to install this version on top of it will damage your system configuration.\n\n"
			"Do you really want to proceed?"
		))
		(default 0)
		(help (cat
			"Your system appears to have the Roadshow TCP/IP stack preinstalled, and unless you are "
			"really, really sure of what you are doing, you should not attempt to overwrite or uninstall it.\n\n"
			"If in doubt, choose 'No' to stop this installation script."
		))
	))
	(
		(exit (quiet))
	))
))

;=============================================================================
; So what do we do now?
;=============================================================================

(welcome "Welcome to the Roadshow TCP/IP stack installation and backup utility!")

; This is for overriding/testing the installation/uninstallation mode.
(set #normal-mode 1)

(set #installed 0)

(while (not #installed)
	(if (and #normal-mode (and (exists "LIBS:bsdsocket.library") (exists "LIBS:usergroup.library")))
	(
		(set #mode (askchoice
			(prompt (cat
				"It appears that Roadshow is currently installed on your system. "
				"Please choose whether you want to save a backup copy of your "
				"Roadshow configuration files, or if you want to uninstall the "
				"Roadshow software."
			))
			(choices "Save a copy of the configuration" "Uninstall Roadshow" "Exit (do nothing of the above)")
			(help @askchoice-help)
		))

		; Option #0 is "Save a copy of the configuration"
		(if (= #mode 0)
		(
			(P_MakeBackup)
		))

		; Option #1 is "Uninstall"
		(if (= #mode 1)
		(
			(P_Uninstall)

			; And exit quietly, since once Roadshow has been uninstalled, you
			; cannot make a copy of the configuration (it's gone) or uninstall
			; it again.
			(exit (quiet))
		))

		; Option #2 is "Exit"
		(if (= #mode 2)
		(
			(exit (quiet))
		))
	)
	(
		(P_Install)
		(set #installed 1)
	))
)

(complete 100)
